<template>
  <el-card class="monitor-indicator-list">
    <tag-content-header add search refresh setting @toolFun="toolFun" :settingData="columns">
      <template #title>
        <span class="title-text">监控配置列表</span>
      </template>
    </tag-content-header>

    <tag-table
      ref="table"
      @refresh="getList"
      :data="dataList"
      :loading="loading"
      :columns="columns"
      :params="params"
      :total="totalSize"
    >
      <template #alarmCount="{ scope }">
        <el-tag :type="scope.row.alarmCount > 0 ? 'warning' : ''" effect="dark">{{ scope.row.alarmCount }}</el-tag>
      </template>
      <template #operation="{ scope }">
        <div class="operation-button-box">
          <el-tooltip effect="dark" content="编辑" placement="top" :enterable="false">
            <icon-svg name="iconEdit" class="svg-edit" @click.native="handleEdit(scope.row)"></icon-svg>
          </el-tooltip>
          <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
            <icon-svg name="iconDelete" class="svg-delete" @click.native="handleDelete(scope.row)"></icon-svg>
          </el-tooltip>
        </div>
      </template>
    </tag-table>
    <config-drawer ref="configDrawer" @callback="getList"></config-drawer>
  </el-card>
</template>
<script>
import configDrawer from './config-drawer'
export default {
  name: 'configList',
  components: {
    configDrawer
  },
  data() {
    return {
      dataList: [],
      loading: false,
      columns: [
        {
          show: true,
          label: '标签名称',
          prop: 'tagNm',
          search: true,
          searchKey: '',
          align: 'left'
        },
        {
          show: true,
          label: '代码值个数',
          prop: 'codeNum',
          align: 'center'
        },
        {
          show: true,
          label: '默认值',
          prop: 'defaultValue',
          align: 'center'
        },
        {
          show: true,
          label: '创建时间',
          prop: 'createTm',
          align: 'center'
        },
        {
          show: 'fixed',
          label: '操作',
          prop: 'operation',
          align: 'center',
          width: 160
        }
      ],
      params: {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {},
        descPriority: false
      },
      totalSize: 0
    }
  },
  methods: {
    getList() {
      this.loading = true
      this.$ajax({
        url: this.$ajax.setUrl('mon/monTagConfig/configPage'),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.dataList = data.data.records
          this.totalSize = data.data.totalSize
        } else {
          this.$message.error(data.data || data.msg)
        }
        this.loading = false
      })
    },
    toolFun(operation) {
      switch (operation) {
        case 'add':
          this.handleEdit()
          break
        case 'search':
          // 搜索动作
          this.$refs['table'].searchToggle()
          break
        case 'refresh':
          // 刷新动作
          this.getList()
          break
      }
    },
    handleEdit(row = {}) {
      this.$refs.configDrawer.showDrawer(row.tagId || '')
    },
    handleDelete(row) {
      this.$confirm(`确定删除【${row.tagNm}】？`).then(() => {
        let messageBox = document.getElementsByClassName('el-message-box__wrapper')
        messageBox[0].focus() // 防止按空格键重复确定
        this.$ajax({
          url: this.$ajax.setUrl(`mon/monTagConfig/deleteConfig/${row.tagId}`),
          method: 'delete',
          params: this.$ajax.setParams()
        }).then(({ data }) => {
          if (data && data.code === '0') {
            this.$message.success(data.msg)
            this.getList()
          } else {
            this.$message.error(data.data || data.msg)
          }
        })
      })
    }
  },
  created() {
    this.getList()
  }
}
</script>

<style lang="scss"></style>
